home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / CBASE102.ARJ / BFLPOP.C < prev    next >
Text File  |  1991-09-23  |  3KB  |  128 lines

  1. /*    Copyright (c) 1989 Citadel    */
  2. /*       All Rights Reserved        */
  3.  
  4. /* #ident    "@(#)bflpop.c    1.5 - 91/09/23" */
  5.  
  6. #include <ansi.h>
  7.  
  8. /* ansi headers */
  9. #include <errno.h>
  10. #ifdef AC_STDDEF
  11. #include <stddef.h>
  12. #endif
  13.  
  14. /* local headers */
  15. #include "blkio_.h"
  16.  
  17. /*man---------------------------------------------------------------------------
  18. NAME
  19.      bflpop - pop block off of free list
  20.  
  21. SYNOPSIS
  22.      #include <blkio.h>
  23.  
  24.      int bflpop(bp, bnp)
  25.      BLKFILE *bp;
  26.      bpos_t *bnp;
  27.  
  28. DESCRIPTION
  29.      The bflpop function gets a block out of the free list of the
  30.      block file associated with BLKFILE pointer bp.  If the free list
  31.      is empty, the block number of the first block past the end of
  32.      file is retrieved.  The block number of the new block is returned
  33.      in the location pointed to by bnp.
  34.  
  35.      Note that if the free list is empty, the same end block will be
  36.      retrieved by flpop until that block is written, extending the
  37.      file length.
  38.  
  39.      bflpop will fail if one or more of the following is true:
  40.  
  41.      [EINVAL]       bp is not a valid BLKFILE pointer.
  42.      [EINVAL]       bnp is the NULL pointer.
  43.      [BEEOF]        bp is empty.
  44.      [BEEOF]        The free list head past points past the end of the
  45.                     file.
  46.      [BENFL]        bp does not have a free list.
  47.      [BENOPEN]      bp is not open for writing.
  48.  
  49. SEE ALSO
  50.      bflpush.
  51.  
  52. DIAGNOSTICS
  53.      Upon successful completion, a value of 0 is returned.  Otherwise,
  54.      a value of -1 is returned, and errno set to indicate the error.
  55.  
  56. NOTES
  57.      To use the free list functions in the blkio library, the first
  58.      element of the file header must be the free list head with type
  59.      bpos_t.  This must be initialized to 0 immediately after the file
  60.      is created and not accessed afterward except using bflpop and
  61.      bflpush.  Also, the block size must be at least sizeof(bpos_t).
  62.  
  63. ------------------------------------------------------------------------------*/
  64. #ifdef AC_PROTO
  65. int bflpop(BLKFILE *bp, bpos_t *bnp)
  66. #else
  67. int bflpop(bp, bnp)
  68. BLKFILE *bp;
  69. bpos_t *bnp;
  70. #endif
  71. {
  72.     bpos_t oldflhno = 0;
  73.     bpos_t newflhno = 0;
  74.  
  75.     /* validate arguments */
  76.     if (!b_valid(bp) || bnp == NULL) {
  77.         errno = EINVAL;
  78.         return -1;
  79.     }
  80.  
  81.     /* check if not open for writing */
  82.     if (!(bp->flags & BIOWRITE)) {
  83.         errno = BENOPEN;
  84.         return -1;
  85.     }
  86.  
  87.     /* check if no free list */
  88.     if (bp->hdrsize < sizeof(oldflhno) || bp->blksize < sizeof(oldflhno)) {
  89.         errno = BENFL;
  90.         return -1;
  91.     }
  92.  
  93.     /* check if header not yet written */
  94.     if (bp->endblk < 1) {
  95.         errno = BEEOF;
  96.         return -1;
  97.     }
  98.  
  99.     /* get block number of current free list head */
  100.     if (bgethf(bp, (size_t)0, &oldflhno, sizeof(oldflhno)) == -1) {
  101.         BEPRINT;
  102.         return -1;
  103.     }
  104.     if (oldflhno >= bp->endblk) {
  105.         errno = BEEOF;
  106.         return -1;
  107.     }
  108.  
  109.     /* if free list empty, get new block at end of file */
  110.     if (oldflhno == 0) {
  111.         oldflhno = bp->endblk;
  112.     } else {    /* else get new free list head */
  113.         if (bgetbf(bp, oldflhno, (size_t)0, &newflhno, sizeof(newflhno)) == -1) {
  114.             BEPRINT;
  115.             return -1;
  116.         }
  117.         if (bputhf(bp, (size_t)0, &newflhno, sizeof(newflhno)) == -1) {
  118.             BEPRINT;
  119.             return -1;
  120.         }
  121.     }
  122.  
  123.     /* load return argument */
  124.     *bnp = oldflhno;
  125.  
  126.     return 0;
  127. }
  128.